import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
import routers from './project/config/router'
const Home = () => import(/* webpackChunkName: "login_home" */ './components/Home.vue')
const Login = () => import(/* webpackChunkName: "login_home" */ './components/Login.vue')
// const BlueBottomFrame = () => import('./components/block/BlueBottomFrame.vue')
const DataChartsPage = () => import('./view/template/DataChartsPage')
const children = [
  {
    path: 'dataChartsPage',
    name: 'dataChartsPage',
    component: DataChartsPage
  },
  {
    path: 'security/list',
    name: 'security-list',
    component: () => import('./view/security/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/security/add',
    name: '/security-add',
    component: () => import('./view/security/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/menu/add',
    name: '/menu-add',
    component: () => import('./view/menu/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/menu/list',
    name: '/menu-list',
    component: () => import('./view/menu/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/api/add',
    name: '/api-add',
    component: () => import('./view/api/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/api/list',
    name: '/api-list',
    component: () => import('./view/api/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/api/edit',
    name: '/api-edit',
    component: () => import('./view/api/Edit')
    // component: () => import('./view/security/List')
  },
  {
    path: '/role/add',
    name: '/role-add',
    component: () => import('./view/role/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/role/list',
    name: '/role-list',
    component: () => import('./view/role/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/role/edit',
    name: '/role-edit',
    component: () => import('./view/role/Edit')
    // component: () => import('./view/security/List')
  },
  // 用户管理
  {
    path: '/user/add',
    name: '/user-add',
    component: () => import('./view/user/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/user/list',
    name: '/user-list',
    component: () => import('./view/user/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/user/edit',
    name: '/user-edit',
    component: () => import('./view/user/Edit')
    // component: () => import('./view/security/List')
  },
  // 服务器管理
  {
    path: '/fileServer/add',
    name: '/fileServer-add',
    component: () => import('./view/fileServer/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileServer/list',
    name: '/fileServer-list',
    component: () => import('./view/fileServer/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileServer/edit',
    name: '/fileServer-edit',
    component: () => import('./view/fileServer/Edit')
    // component: () => import('./view/security/List')
  },
  // 附件类别管理
  {
    path: '/fileType/add',
    name: '/fileType-add',
    component: () => import('./view/fileType/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileType/list',
    name: '/fileType-list',
    component: () => import('./view/fileType/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileType/edit',
    name: '/fileType-edit',
    component: () => import('./view/fileType/Edit')
    // component: () => import('./view/security/List')
  },
  // 附件记录管理
  {
    path: '/fileRecord/add',
    name: '/fileRecord-add',
    component: () => import('./view/fileRecord/Add')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileRecord/list',
    name: '/fileRecord-list',
    component: () => import('./view/fileRecord/List')
    // component: () => import('./view/security/List')
  },
  {
    path: '/fileRecord/edit',
    name: '/fileRecord-edit',
    component: () => import('./view/fileRecord/Edit')
    // component: () => import('./view/security/List')
  }
]
if (routers) {
  routers.forEach(e => children.push(e))
}
// 使用懒加载技术
// 1、引入
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children
  }
  // ,
  // {
  //   path: '/test',
  //   name: 'transparentBlock',
  //   component: SecurityList
  // }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (!to.name) {
    Message({ type: 'error', message: '此页面不存在' })
    return next('/home')
  }
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    Message({
      type: 'warning',
      message: '请先登录！！！'
    })
    return next('/login')
  }

  next()
})

export default router
